<?php
/**
 * @file $HeadURL: link.php $
 * @author $Author: Jim.Chou (zhouzhe@haibao.com) $
 * @date $Date: 2015年4月7日 14:33:48 $
 * @skip in my loul
 */
namespace haibao\cms\business;
use haibao\cms\business\BusinessException;
use \haibao\cms\data\link as LinkData; 

class Link extends \haibao\cms\business\Base{
	
	public static function addLink($model){
		self::checkOperation(self::FUNC_CODE_LINK_ADD);
		if(!$model){
			throw new BusinessException('当前友情链接无效');
		}
		if(!$model->LinkName){
			throw new BusinessException('无效链接名');
		}
		if(!$model->Url){
			throw new BusinessException('无效链接');
		}
		$linkData = new LinkData();
		$linkData->add($model);
		self::saveOperateHistory(self::FUNC_CODE_LINK_ADD, $model);
	}
	
	public static function editLink($model){
		self::checkOperation(self::FUNC_CODE_LINK_EDIT);
		if(!$model){
			throw new BusinessException('当前友情链接无效');
		}
		if(!$model->LinkName){
			throw new BusinessException('无效链接名');
		}
		if(!$model->Url){
			throw new BusinessException('无效链接');
		}
		$linkData = new LinkData();
		
		$linkData->updateById($model->Id,$model);
		//添加历史记录
		self::saveOperateHistory(self::FUNC_CODE_LINK_EDIT, $model->toArray(false));
	}
	
	public static function getLinkById($linkId){
		if(!$linkId){
			return NULL;
		}
		$linkData = new LinkData();
		return $linkData->getModelById($linkId);
	}
	public static function getLinkList($linkFilter){
		$linkData = new LinkData();
		return $linkData->getLinkList($linkFilter);
	}
	
	public static function delLink($id){
		self::checkOperation(self::FUNC_CODE_LINK_DEL);
		if (!$id){
			throw new BusinessException('友情链接Id不能为空');
		}
		$linkData = new LinkData();
		$linkData->delLink($id,\haibao\cms\model\data\Link::STATUS_DELETE);
		
		self::saveOperateHistory(self::FUNC_CODE_LINK_DEL, array($id));
	}
	public static function recLink($id){
		self::checkOperation(self::FUNC_CODE_LINK_EDIT);
		if (!$id){
			throw new BusinessException('友情链接Id不能为空');
		}
		$linkData = new LinkData();
		$linkData->delLink($id,\haibao\cms\model\data\Link::DEFAULT_STATUS_OK);
		
		self::saveOperateHistory(self::FUNC_CODE_LINK_DEL, array($id));
	}

}
